package com.txw.javaCore.server.service;

import com.txw.javaCore.model.entity.User;
import com.txw.javaCore.model.mapper.UserMapper;
import com.txw.javaCore.model.request.UserLoginRequest;
import com.txw.javaCore.server.mq.LogPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @PROJECT_NAME: JavaCoreB2
 * @DESCRIPTION: 用户服务
 * @Author: 涂玄武
 * @DATE: 2021/2/22 13:37
 */
@Service
@Slf4j
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogPublisher logPublisher;

    /**
     * 用户登录服务
     *
     * @param request
     * @return
     * @throws Exception
     */
    public Boolean login(UserLoginRequest request) throws Exception {
        //根据用户名和密码查询用户实体记录
        Optional<User> optional = Optional.ofNullable(userMapper.selectByUserNamePassword(request.getUserName(), request.getPassword()));
        if (optional.isPresent()) {
            //表示数据表中存在该用户，并且密码是匹配的
            User user = optional.get();
            request.setUserId(user.getId());
            //发送登录成功日志信息
            logPublisher.sendLogMsg(request);
            //返回登录成功
            return true;
        } else {
            //返回登录失败
            return false;
        }
    }
}
